package com.taxshare.purchaser.service.rulesReview;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taxshare.purchaser.constant.CodeConstant;
import com.taxshare.purchaser.entity.InvoiceCollectionEntity;
import com.taxshare.purchaser.entity.InvoiceTaxRulesEntity;
import com.taxshare.purchaser.mapper.BlackWhiteMapper;
import com.taxshare.purchaser.mapper.InvoiceTaxRulesMapper;
import com.taxshare.purchaser.service.CommService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: DefaultReviewCheck
 * @Description: 默认规则
 * @author: hjh
 * @date: 2021/6/17 15:54
 */
@Component
public class DefaultReviewCheck implements RuleMainHandler {

    @Autowired
    BlackWhiteMapper blackWhiteMapper;

    @Autowired
    InvoiceTaxRulesMapper invoiceTaxRulesMapper;

    @Autowired
    CommService commService;
    /**
     * 默认规则
     *
     * @param bean
     * @param id
     * @return
     */
    @Override
    public CheckInfoRespVO checkRule(InvoiceCollectionEntity bean, String id) {
        InvoiceTaxRulesEntity info = getInfo(id);
        CheckInfoRespVO vo = new CheckInfoRespVO();
        Integer number = 1;
        List<String> taxNos= new ArrayList<>();
        if (info != null) {
            //购房抬头非个人则进行校验
            if(bean.getPurchaserName().length() > 4){
                if (CodeConstant.COMM_CODE_1.equals(info.getTaxType())) {
                    taxNos = commService.getTaxNumbers();
                    if (!taxNos.contains(bean.getPurchaserTaxNo())) {
                        vo.setGoing(false);
                        vo.setFlag(false);
                        vo.setMsg(info.getTaxNotPassReasons());
                        vo.setPass(CodeConstant.RULE_4);
                    }
                }else if(CodeConstant.COMM_CODE_2.equals(info.getTaxType())){
                    number = blackWhiteMapper.selectOrganize(bean);
                    if (number.intValue() == 0 ) {
                        vo.setGoing(false);
                        vo.setFlag(false);
                        vo.setMsg(info.getTaxNotPassReasons());
                        vo.setPass(CodeConstant.RULE_4);
                    }
                }
            }
        }
        return vo;
    }

    public CheckInfoRespVO checkSubmit(String id) {
        CheckInfoRespVO vo = new CheckInfoRespVO();
        InvoiceTaxRulesEntity info = getInfo(id);
        if (info!=null&& CodeConstant.COMM_CODE_1.equals(info.getForceFlag())){
            vo.setGoing(false);
            vo.setMsg("购方税号规则强制校验不通过!");
        }
        return vo;
    }

    /**
     * 查询规则开启状态
     *
     * @param pid
     * @return 税号规则
     */
    private InvoiceTaxRulesEntity getInfo(String pid) {
        InvoiceTaxRulesEntity entity = invoiceTaxRulesMapper.selectOne(new QueryWrapper< InvoiceTaxRulesEntity >().eq("uuid", pid));
        if (entity != null && CodeConstant.RULES_OPEN.equals(entity.getTaxIsOpen())) {
            return entity;
        }
        return null;
    }
}
